import java.io.*;
import java.text.Collator;
import java.util.*;

public class TextSorter {

    public static void main(String[] args) {
        String inputFilePath = "input.txt"; // 输入文件路径
        String outputFilePath = "output.txt"; // 输出文件路径

        try {
            List<String> lines = readLines(inputFilePath);
            Collections.sort(lines, new LineComparator());
            writeLines(lines, outputFilePath);
            System.out.println("排序完成，结果已写入 " + outputFilePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 读取文件内容
    private static List<String> readLines(String filePath) throws IOException {
        List<String> lines = new ArrayList<>();
        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = reader.readLine()) != null) {
                lines.add(line);
            }
        }
        return lines;
    }

    // 写入文件内容
    private static void writeLines(List<String> lines, String filePath) throws IOException {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
            for (String line : lines) {
                writer.write(line);
                writer.newLine();
            }
        }
    }

    // 自定义比较器
    static class LineComparator implements Comparator<String> {

        private Collator collator = Collator.getInstance(java.util.Locale.CHINA); // 中文排序

        @Override
        public int compare(String line1, String line2) {
            String firstChar1 = getFirstChar(line1);
            String firstChar2 = getFirstChar(line2);

            if (isDigit(firstChar1) && isDigit(firstChar2)) {
                return Integer.parseInt(firstChar1) - Integer.parseInt(firstChar2);
            } else if (isDigit(firstChar1)) {
                return -1; // 数字排在最前
            } else if (isDigit(firstChar2)) {
                return 1;
            } else if (isLetter(firstChar1) && isLetter(firstChar2)) {
                return firstChar1.compareToIgnoreCase(firstChar2);
            } else if (isLetter(firstChar1)) {
                return -1; // 字母排在数字之后
            } else if (isLetter(firstChar2)) {
                return 1;
            } else {
                return collator.compare(line1, line2); // 中文排序
            }
        }

        // 获取首字符
        private String getFirstChar(String line) {
            if (line == null || line.isEmpty()) {
                return "";
            }
            return String.valueOf(line.charAt(0));
        }

        // 判断是否为数字
        private boolean isDigit(String str) {
            if (str == null || str.isEmpty()) {
                return false;
            }
            return str.matches("\\d+");
        }

        // 判断是否为字母
        private boolean isLetter(String str) {
            if (str == null || str.isEmpty()) {
                return false;
            }
            return str.matches("[a-zA-Z]+");
        }
    }
}